package model.histoire;

import java.util.ArrayList;
import java.util.LinkedHashMap;

import model.ePackagePath;
import tool.externalResLoader.AutoLoader;
import tool.externalResLoader.absTreeFileLoader;
import controleur.Config;
import controleur.GameControleur;
import controleur.eMenuOption;
import exception.AutoLoadErrorException;
import exception.FileFormatException;
import exception.GameStoryStructureErrorException;
import exception.InvalidIDException;
import exception.PathErrorException;
import exception.SequenceEndException;

/**
 * Classe representant une sequence, c'est a dire un ensemble de transitions
 */
public class Sequence extends absTransition
{

	private absTransition currentTransition;
	private final ArrayList<absTransition> transitions;

	public Sequence(absTreeFileLoader r) throws FileFormatException
	{
		super(r);
		this.transitions = new ArrayList<absTransition>();

		ArrayList<String> paths = new ArrayList<String>();
		paths.add(ePackagePath.evenementTransition.getPath());
		paths.add(ePackagePath.histoireTransition.getPath());

		try
		{

			for (absTreeFileLoader i : r.moveToPath("transitions", false)
					.getElements())
				this.transitions.add((absTransition) AutoLoader.load(paths, i));

			this.currentTransition = this.getWithId(r.getElementInt("first"));
		}
		catch (PathErrorException e)
		{
			throw new FileFormatException("File stucture error");
		}
		catch (AutoLoadErrorException e)
		{
			throw new FileFormatException(
					"File stucture error, unable to initialise object from content ("
							+ e.getMessage() + ")");
		}
		catch (InvalidIDException e)
		{
			throw new FileFormatException(
					"File stucture error, first id is wrong");
		}
	}

	public Sequence(Integer id, String titre, String desc,
			LinkedHashMap<Integer, String> suivantes, absTransition firstTransition,
			ArrayList<absTransition> transitions)
	{
		super(id, titre, desc, suivantes);
		this.currentTransition = firstTransition;
		this.transitions = transitions;
	}

	@Override
	public void execute(GameControleur c, Config cfg)
	{
	}

	public absTransition getCurrent()
	{
		return this.currentTransition;
	}

	private absTransition getWithId(Integer id) throws InvalidIDException
	{
		for (absTransition t : this.transitions)
			if (t.id == id)
				return t;

		throw new InvalidIDException(id);
	}

	public absTransition goNext(eMenuOption e)
			throws GameStoryStructureErrorException, SequenceEndException
	{
		Integer nextId = this.getCurrent().getNext(e);
		
		if(nextId < 0)
			throw new SequenceEndException("No next !");

		try
		{
			this.currentTransition = this.getWithId(nextId);
		}
		catch (InvalidIDException e1)
		{
			throw new GameStoryStructureErrorException(
					"goNext, next id do not exist");
		}
		return this.getCurrent();
	}

	@Override
	public absTreeFileLoader save(absTreeFileLoader r)
	{
		super.save(r);
		try
		{
			r.addElement("first", this.currentTransition.toString());
			absTreeFileLoader trs = r.moveToPath("transitions", true);
			for (absTransition i : this.transitions)
				trs.addElement(null, i.save(r.getClass().getConstructor()
						.newInstance()));
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
		return r;

	}

	public boolean isFinished()
	{
		return this.suivantes.size() == 0;
	}
	
	@Override
	public String toString()
	{
		String id_s = "";
		for (absTransition i : this.transitions)
			id_s += i.id + " ";

		return super.toString() + ",TransitionsID:" + id_s;
	}

	public void dump()
	{
		for (absTransition t : this.transitions)
			System.out.println(t);
	}

}
